/**
 *
 */
package com.person.modules.person.service.impl;
import com.alibaba.druid.sql.visitor.functions.Insert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.person.common.utils.Constant;
import com.person.common.utils.PageUtils;
import com.person.common.utils.Query;
import com.person.modules.person.dao.WorkRecordDao;
import com.person.modules.person.entity.UserDocEntity;
import com.person.modules.person.entity.UserPlanEntity;
import com.person.modules.person.entity.WorkRecordEntity;
import com.person.modules.person.service.WorkRecordService;
import com.person.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("workRecordService")
public class WorkRecordServiceImpl extends ServiceImpl<WorkRecordDao, WorkRecordEntity> implements WorkRecordService {
    @Autowired
    SysUserService userService;
    @Autowired
    WorkRecordDao workRecordDao;
	private Workbook wb;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String workMonth = (String) params.get("workMonth");
        String workDate = (String) params.get("workDate");
        String status = (String) params.get("status");
        Long userId = (Long) params.get("userId");

        IPage<WorkRecordEntity> page = this.page(
                new Query<WorkRecordEntity>().getPage(params),
                new QueryWrapper<WorkRecordEntity>()
                        .eq(StringUtils.isNotBlank(workDate), "work_date", workDate)
                        .eq(StringUtils.isNotBlank(workMonth), "work_month", workMonth)
                        .eq(StringUtils.isNotBlank(status), "status", status)
                        .eq(userId != null, "user_id", userId)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER))
        );
        List<WorkRecordEntity> records = page.getRecords();

        List<WorkRecordEntity> list = new ArrayList<WorkRecordEntity>();
        for (WorkRecordEntity r : records) {
            list.add(r);
        }
        page.setRecords(list);
        return new PageUtils(page);
    }

    public void deleteBatch(Long[] ids) {
        this.removeByIds(Arrays.asList(ids));

    }

    @Override
    public void update(Workbook wb) {
    	List<WorkRecordEntity> wRecord = new ArrayList<WorkRecordEntity>();
    
    	 Sheet sheet = wb.getSheetAt(0); //默认取第一个sheet 	         
		    //int colsNum = sheet.getPhysicalNumberOfRows();  //获取实际的行数	       
    	 	int rowsNum = sheet.getLastRowNum();
		    for(int j=1; j<rowsNum+1;j++) //第一行为表头，所以从第二行开始
		    {// getLastRowNum，获取最后一行的行标
		     Row row = sheet.getRow(j);
		     if (row != null) {
				WorkRecordEntity wOE= new WorkRecordEntity();
				wOE.setUserId( (long)Double.parseDouble(row.getCell(0).toString()));
				wOE.setCompanyId((long)Double.parseDouble(row.getCell(1).toString()));
				wOE.setWorkMonth((int)Double.parseDouble(row.getCell(2).toString()));
				wOE.setWorkLate((int)Double.parseDouble(row.getCell(3).toString()));
				wOE.setWorkEarly((int)Double.parseDouble(row.getCell(4).toString()));
				wOE.setWorkOvertime((int)Double.parseDouble(row.getCell(5).toString()));
				wOE.setAbsenteeismDays((int)Double.parseDouble(row.getCell(6).toString()));
				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
				wOE.setCreateTime(sdf.format(date));
				workRecordDao.insert(wOE);
		     	}
			}
		    
		
    }

    @Override
    public List<WorkRecordEntity> getListByUserId(long userId, long companyId) {
       List<WorkRecordEntity> list =workRecordDao.getListByUserId(userId,companyId);
       return list;
    }
}

